package com.fht.service.food.impl;

import com.fht.constant.ManagermentSystemConstant;
import com.fht.entity.FoodCategory;
import com.fht.entity.FoodRecord;
import com.fht.from.FoodCategoryFrom;
import com.fht.mapper.food.FoodRecordMapper;
import com.fht.service.food.FoodCategoryService;
import com.fht.service.food.FoodRecordService;


import com.fht.service.inventory.InventoryParticularsService;
import com.fht.vo.LayUITableVO;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class FoodRecordServiceImpl implements FoodRecordService {

    @Resource
    public FoodRecordMapper foodRecordMapper;
    @Resource
    public FoodCategoryService foodCategoryService;
    @Autowired
    public InventoryParticularsService inventoryParticularsService;


    @Override
    public LayUITableVO<List<FoodRecord>> selectByFrom(Map map) {


        FoodCategoryFrom from = (FoodCategoryFrom) map.get("from");

        List<String> recordCategoryList = new ArrayList<>();
        if (!"0".equals(from.getRecordCategoryId())){
            List<FoodCategory> foodCategoryList = foodCategoryService.findByParentId(from.getRecordCategoryId());
            if (foodCategoryList.size()==0){
                recordCategoryList.add(from.getRecordCategoryId());
            }else {
                for (FoodCategory foodCategory : foodCategoryList) {
                recordCategoryList.add(String.valueOf(foodCategory.getId()));
                }
            }

        }
        map.put("recordCategoryList",recordCategoryList);

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));


        List<FoodRecord> list = foodRecordMapper.findPage(map);

        PageInfo<FoodRecord> pageInfo = new PageInfo<>(list);

        for (FoodRecord foodRecord : list) {
            Integer price = foodRecord.getPrice();
            foodRecord.setPriceString(String.valueOf(price/ ManagermentSystemConstant.PRICE_TRANSITION));
            if ("1".equals(foodRecord.getStatus())){
                foodRecord.setStatus("上架");
            }else if ("0".equals(foodRecord.getStatus())){
                foodRecord.setStatus("下架");
            }
            if ("1".equals(foodRecord.getPrintLabel())){
                foodRecord.setPrintLabel("开启");
            }else if ("0".equals(foodRecord.getPrintLabel())){
                foodRecord.setPrintLabel("关闭");
            }
            if (foodRecord.getFirmOffers()!=null && foodRecord.getReturnsAmount()!=null) {
                foodRecord.setSellTotal(String.valueOf(foodRecord.getPrice() * (foodRecord.getSalesVolume() - foodRecord.getReturnsAmount()) / 100.0));
            }

        }

        LayUITableVO vo = new LayUITableVO();
        vo.setCount(pageInfo.getTotal());
        vo.setData(list);

        return vo;

    }

    @Override
    public FoodRecord selectById(String id) {
        FoodRecord foodRecord = foodRecordMapper.selectByPrimaryKey(id);
        foodRecord.setPriceString(String.valueOf(foodRecord.getPrice()/ ManagermentSystemConstant.PRICE_TRANSITION));

        String specialPriceStartDate = foodRecord.getSpecialPriceStartDate();
        if (!"".equals(specialPriceStartDate) && null != specialPriceStartDate) {
            specialPriceStartDate = specialPriceStartDate.substring(0, specialPriceStartDate.lastIndexOf("."));
            foodRecord.setSpecialPriceStartDate(specialPriceStartDate);
        }

        String specialPriceEndDate = foodRecord.getSpecialPriceEndDate();

        if (!"".equals(specialPriceEndDate) && null != specialPriceEndDate) {
            specialPriceEndDate = specialPriceEndDate.substring(0, specialPriceEndDate.lastIndexOf("."));
            foodRecord.setSpecialPriceEndDate(specialPriceEndDate);
        }
        return foodRecord;
    }

    @Override
    @Transactional
    public void update(FoodRecord foodRecord) {

        String priceString = foodRecord.getPriceString();
        foodRecord.setPrice((int) (Double.valueOf(priceString)*100));
        FoodCategory foodCategory = foodCategoryService.selectById(String.valueOf(foodRecord.getCategoryId()));
        foodRecord.setCategoryName(foodCategory.getChild());
        if ("".equals(foodRecord.getSpecialPriceStartDate())){
            foodRecord.setSpecialPriceStartDate(null);
        }
        if ("".equals(foodRecord.getSpecialPriceEndDate())){
            foodRecord.setSpecialPriceEndDate(null);
        }

        foodRecordMapper.updateByPrimaryKeySelective(foodRecord);
    }

    @Override
    @Transactional
    public void delelteById(String id) {
        foodRecordMapper.deleteByPrimaryKey(id);

    }

    @Override
    @Transactional
    public void add(FoodRecord foodRecord) {
        String priceString = foodRecord.getPriceString();
        foodRecord.setPrice((int) (Double.valueOf(priceString)*100));
        FoodCategory foodCategory = foodCategoryService.selectById(String.valueOf(foodRecord.getCategoryId()));
        foodRecord.setCategoryName(foodCategory.getChild());
        if ("".equals(foodRecord.getSpecialPriceStartDate())){
            foodRecord.setSpecialPriceStartDate(null);
        }
        if ("".equals(foodRecord.getSpecialPriceEndDate())){
            foodRecord.setSpecialPriceEndDate(null);
        }
        foodRecordMapper.insertSelective(foodRecord);

    }

    @Override
    @Transactional
    public void updateRepertory(FoodRecord foodRecord) {
        FoodRecord old = foodRecordMapper.selectByPrimaryKey(foodRecord.getId());

        Integer change = foodRecord.getRepertory()-old.getRepertory();

        if ((int)change !=0) {

            inventoryParticularsService.save(foodRecord.getId(),old.getRepertory(), change);
        }

        foodRecordMapper.updateByPrimaryKeySelective(foodRecord);

    }

    @Override
    public void updateRepertory(Integer id, String orderId, Integer change) {
        FoodRecord old = foodRecordMapper.selectByPrimaryKey(id);
        Integer oldRepertory = old.getRepertory();
        old.setRepertory((int)old.getRepertory()+change);
        foodRecordMapper.updateByPrimaryKeySelective(old);

        inventoryParticularsService.save(id,oldRepertory,change,orderId);

    }
}
